package com.example.bishe.toolclass;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yws
 * @Date 2022/4/4 14:01
 */
@Component
public class CUD {
    @Resource
    GitTonk gitTonk;
    @Resource
    ApiReturnDate apiReturnDate;
    @Resource
    JsonValue jsonValue;
    /*以下为对标签的操作*/
    public HashMap addTag(String tagId,String tagName){
        String access_token = gitTonk.AccessToken("useraccess_token");
        String user = MyEnum.TAG_CAOZUO.getValue() + "create?" +
                "access_token=" + access_token;
        Map<String, String> maps = new HashMap<>();
        maps.put("tagname", tagName);
        if (tagId!= null) {
            maps.put("tagid", tagId);
        }
        String str = JSON.toJSONString(maps);
        //System.out.println(str);
        //根据url和请求参数获得返回JSON数据
        JSONObject mydate = apiReturnDate.Mydate(user, str);
        HashMap value = jsonValue.getValue(mydate, "errcode", "errmsg", "tagid");
        return value;
    }

    public String addTagPan(HashMap value){
        String errmsg="";
        String x = value.get("errcode")+"";
        if (x.equals("40068"))
            errmsg = "不合法的标签/标签组ID,标签id已经被使用过";
        if (x.equals("40071"))
            errmsg = "标签名称已经存在";
        return errmsg;
    }

    public HashMap updateTag(String tagId,String tagName){
        String access_token = gitTonk.AccessToken("useraccess_token");
        String user = MyEnum.TAG_CAOZUO.getValue() + "update?" +
                "access_token=" + access_token;
        Map<String, String> maps = new HashMap<>();
        maps.put("tagname", tagName);
        maps.put("tagid", tagId);
        String str = JSON.toJSONString(maps);
        JSONObject mydate = apiReturnDate.Mydate(user, str);
        System.out.println(mydate);
        HashMap value = jsonValue.getValue(mydate, "errcode", "errmsg", "tagid");
        return value;
    }
    public String updateTagPan(HashMap value){
        String errmsg="";
        String x = value.get("errcode")+"";
        if (x.equals("40068"))
            errmsg = "不合法的标签/标签组ID,标签id不存在";
        if (x.equals("40071"))
            errmsg = "标签名称已经存在";
        System.out.println(x);
        return errmsg;
    }
    public HashMap deleteTag(String tagId){
        String access_token = gitTonk.AccessToken("useraccess_token");
        String user = MyEnum.TAG_CAOZUO.getValue() + "delete?" +
                "access_token=" + access_token+"&tagid="+tagId;
        JSONObject mydate = apiReturnDate.Mydate(user);
        System.out.println(mydate);
        HashMap value = jsonValue.getValue(mydate, "errcode", "errmsg");
        return value;
    }
    public String deleteTagPan(HashMap value){
        String errmsg="";
        String x = value.get("errcode")+"";
        if (x.equals("40068"))
            errmsg = "不合法的标签/标签组ID,标签id不存在";
        System.out.println(x);
        return errmsg;
    }

    public HashMap addTagUser(AddTagUser addTagUser){
        String access_token = gitTonk.AccessToken("useraccess_token");
        String user = MyEnum.TAG_CAOZUO.getValue() + "addtagusers?" +
                "access_token=" + access_token;
        String string = JSON.toJSONString(addTagUser);
        JSONObject mydate = apiReturnDate.Mydate(user,string);
        System.out.println(mydate);
        HashMap value = jsonValue.getValue(mydate, "errcode", "errmsg","invalidlist","invalidparty");
        return value;
    }
    public String addTagUserpan(HashMap value){
        String errmsg="";
        String x = value.get("errcode")+"";
        if (x.equals("40070"))
            errmsg = "指定的标签范围结点全部无效。确认：1）指定的参数格式是否正确。2）指定的成员或者部门，是否存在于通讯录中。";
        if (x.equals("40068"))
            errmsg = "不合法的标签/标签组ID,标签id不存在";
        System.out.println(x);
        return errmsg;
    }/*deltagusers*/
    public HashMap deleteTagUser(AddTagUser addTagUser){
        String access_token = gitTonk.AccessToken("useraccess_token");
        String user = MyEnum.TAG_CAOZUO.getValue() + "deltagusers?" +
                "access_token=" + access_token;
        String string = JSON.toJSONString(addTagUser);
        JSONObject mydate = apiReturnDate.Mydate(user,string);
        System.out.println(mydate);
        HashMap value = jsonValue.getValue(mydate, "errcode", "errmsg","invalidlist","invalidparty");
        return value;
    }
    /*以下为对部门的操作*/
    public HashMap add(String name,String parentid,String wx_id){
        //获取请求的token
        String access_token = gitTonk.AccessToken("useraccess_token");
        //拼接请求url
        String user = MyEnum.DEPARTMENT_CAOZUO.getValue() + "create?" +
                "access_token=" + access_token;
        Map<String, String> maps = new HashMap<>();
        maps.put("name", name);
        maps.put("parentid", parentid);
        if (wx_id!= null) {
            maps.put("id", wx_id);
        }
        String str = JSON.toJSONString(maps);
        //System.out.println(str);
        //根据url和请求参数获得返回JSON数据
        JSONObject mydate = apiReturnDate.Mydate(user, str);
        System.out.println(mydate);
        //获取返回状态
        HashMap value = jsonValue.getValue(mydate, "errcode", "errmsg", "id");
        return value;
    }

    public HashMap update(String id,String name,String parentid){
        String access_token = gitTonk.AccessToken("useraccess_token");
        String user = MyEnum.DEPARTMENT_CAOZUO.getValue() + "update?" +
                "access_token=" + access_token;

        Map<String, String> maps = new HashMap<>();
        maps.put("id",id);
        if(name!=null)
            maps.put("name", name);
        if (parentid!=null)
            maps.put("parentid", parentid);
        String str = JSON.toJSONString(maps);
        //根据url和请求参数获得返回JSON数据
        JSONObject mydate = apiReturnDate.Mydate(user,str);
        HashMap value = jsonValue.getValue(mydate, "errcode", "errmsg");
        return value;
    }

    public HashMap delete(String wx_id){
        //获取请求的token
        String access_token = gitTonk.AccessToken("useraccess_token");
        String user = MyEnum.DEPARTMENT_CAOZUO.getValue() + "delete?" +
                "access_token=" + access_token + "&id=" + wx_id;
        //根据url和请求参数获得返回JSON数据
        JSONObject mydate = apiReturnDate.Mydate(user);
        HashMap<String, String> value = jsonValue.getValue(mydate, "errcode", "errmsg");
        return value;
    }

    public String addpan(HashMap value){
        String errmsg="";
        if (value.get("errcode").equals("60010"))
            errmsg += "部门存在循环关系。请确认：创建部门和更新部门时，指定的parentid参数不能是部门id 或者子部门id";
        if (value.get("errcode").equals("60009"))
            errmsg += "部门名称含有非法字符,不能含有 \\:?*\"<>| 等字符";
        if (value.get("errcode").equals("60123"))
            errmsg += "可能你添加的是根部门id，请更换";
        if (value.get("errcode").equals("60008"))
            errmsg += "部门ID或者部门名称已存在，添加失败。";
        if (value.get("errcode").equals("60004"))
            errmsg += "你所填写的父部门找不到。";
        return errmsg;
    }
    public String deletepan(HashMap value){
        String errmsg="";
        if (value.get("errcode").equals("60005"))
            errmsg += "部门下存在成员，不允许删除有成员的部门。";
        if (value.get("errcode").equals("60006"))
            errmsg += "部门下存在子部门，不允许删除有子部门的部门。";
        if (value.get("errcode").equals("60007"))
            errmsg += "不能删除根部门。";
        if (value.get("errcode").equals("60123"))
            errmsg += "无效的部门id,部门不存在通讯录中。";
        return errmsg;
    }
    public String updatepan(HashMap value){
        String errmsg="";
        if(value.get("errcode").equals("60003"))
            errmsg+="部门ID不存在,需要确认部门ID是否存在通讯录中";
        if(value.get("errcode").equals("60008"))
            errmsg+="部门已存在,部门ID或者部门名称已存在";
        if(value.get("errcode").equals("60124"))
            errmsg+="无效的父部门id，父部门不存在通讯录中";
        return errmsg;
    }
/*以下为对人员的操作*/
    public HashMap addstudent_jzg(String suerid,String name,String gender,String email,String department,String phone){
        String access_token = gitTonk.AccessToken("useraccess_token");
        //拼接访问的URL
        String user =  MyEnum.USER_CAOZUO.getValue()+"create?" +
                "access_token="+access_token;
        Map<String, String> map = new HashMap<>();
        map.put("userid",suerid);
        map.put("name",name);
        map.put("gender",gender);
        map.put("email",email);
        map.put("department",department);
        map.put("mobile",phone);
        String str = JSON.toJSONString(map);
        System.out.println(str);
        //根据url和请求参数获得返回JSON数据
        JSONObject mydate = apiReturnDate.Mydate(user,str);
        System.out.println(mydate);
        HashMap value = jsonValue.getValue(mydate, "errcode", "errmsg");
        return value;
    }
    public String addstudent_jzgpan(HashMap value){
        String errmsg="";
        if(value.get("errcode").equals("60102"))
            errmsg+="用户已经关注过中工学子。";
        if(value.get("errcode").equals("60104"))
            errmsg+="电话号码已经被占用。"+value.get("errmsg");
        if(value.get("errcode").equals("60129"))
            errmsg+="手机号码和邮箱不能都为空。";
        if(value.get("errcode").equals("40058"))
            errmsg+="邮箱格式不正确";
        return errmsg;
    }

    public HashMap deletestudent_jzg(String userid){
        String access_token = gitTonk.AccessToken("useraccess_token");
        String user = MyEnum.USER_CAOZUO.getValue() + "delete?" +
                "access_token=" + access_token + "&userid=" + userid;
        //根据url和请求参数获得返回JSON数据
        JSONObject mydate = apiReturnDate.Mydate(user);
        HashMap<String, String> value = jsonValue.getValue(mydate, "errcode", "errmsg");
        System.out.println(mydate);
        return value;
    }
    public String deletestudent_jzgpan(HashMap value){
        String errmsg="";
        if(value.get("errcode").equals("60111"))
            errmsg+="UserID不存在，用户id为空，或者不存在通讯录中";
        return errmsg;
    }

    public HashMap updatestudent_jzg(String suerid,String name,String gender,String email,String department,String phon){
        String access_token = gitTonk.AccessToken("useraccess_token");
        String user = MyEnum.USER_CAOZUO.getValue() + "update?" +
                "access_token=" + access_token;
        Map<String, String> map = new HashMap<>();
        map.put("userid",suerid);
        if (name!=null)
        map.put("name",name);
        if (gender!=null)
        map.put("gender",gender);
        if (email!=null)
        map.put("email",email);
        if (department!=null)
        map.put("department",department);
        if (phon!=null)
            map.put("mobile",phon);
        String str = JSON.toJSONString(map);
        //根据url和请求参数获得返回JSON数据
        JSONObject mydate = apiReturnDate.Mydate(user,str);
        System.out.println(mydate);
        HashMap value = jsonValue.getValue(mydate, "errcode", "errmsg");
        return value;
    }
    public String updatestudent_jzgpan(HashMap value){
        String errmsg="";
        if(value.get("errcode").equals("60111"))
            errmsg+="UserID不存在，用户id为空，或者不存在通讯录中";
        if(value.get("errcode").equals("40058"))
            errmsg+="邮箱格式不正确";
        if(value.get("errcode").equals("60104")){
            String str = (String) value.get("errmsg");
            errmsg+="电话号码已经被 "+str.substring(15,str.length())+"占用。";
        }if(value.get("errcode").equals("60103")){
            errmsg+="手机号格式不正确";
        }
        return errmsg;
    }
    public HashMap selectAllJg(){
        String access_token = gitTonk.AccessToken("useraccess_token");
        String user = MyEnum.DEPARTMENT_CAOZUO.getValue() + "list?" +
                "access_token=" + access_token;
        JSONObject mydate = apiReturnDate.Mydate(user);
        System.out.println(mydate);
        HashMap value = jsonValue.getValue(mydate, "errcode", "errmsg","department");
        return value;
    }
}
